package com.scs.application.modules.dept.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.modules.dept.dto.DailyBalanceVo;
import com.scs.application.modules.dept.service.DailyBalanceItemService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.dept.service.DailyBalanceService;
import com.scs.application.modules.dept.entity.DailyBalance;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仓库日清表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/daily_balance")
public class DailyBalanceController extends CrudController<DailyBalanceService, DailyBalance>{

    @Autowired
    private DailyBalanceItemService dailyBalanceItemService;


    @GetMapping("/preview")
    public void preview(@RequestParam(name = "id") String id,
                        HttpServletResponse response) throws IOException {
        service.preview(id, response);
    }


    /**
     * 保存日结
     */
    @PostMapping("/save_daily_balance")
    public RestResult saveItem(@RequestBody DailyBalance dailyBalance) {
//        保存前检查当前日期是否月结异常
        String balanceDate = DateUtils.format(dailyBalance.getBalanceDate(), "yyyy-MM-dd");
//        异常删除
//        List<DailyBalanceItem> dailyBalanceItemList = dailyBalanceItemService
//                .list(new QueryWrapper<DailyBalanceItem>()
//                        .lt("balance_date", balanceDate).ne("type",0).orderByAsc("balance_date"));
//        if (!CollectionUtils.isEmpty(dailyBalanceItemList)) {
//            DailyBalanceItem dailyBalanceItem = dailyBalanceItemList.get(0);
//            return RestResult.error(DateUtils.format(dailyBalanceItem.getBalanceDate(), "yyyy-MM-dd")+"月结异常,请重新月结",
//                    DateUtils.format(dailyBalanceItem.getBalanceDate(), "yyyy-MM-dd"));
//        }
        boolean isOk = service.saveDailyDalance(dailyBalance);
        if (!isOk) {
            return RestResult.error("数据保存失败或者仓库没有日结数据");
        }
        DailyBalance entity = service.getOne(new QueryWrapper<DailyBalance>()
                .eq("balance_date", DateUtils.format(new Date(), "yyyy-MM-dd"))
                .eq("warehouse_id", dailyBalance.getWarehouseId())
                .last("limit 1"));
        return RestResult.ok("数据保存成功",entity);
    }
    /**
     * 保存日结
     */
    @PostMapping("/get_daily_balance")
    public RestResult getItemSum(@RequestBody DailyBalanceVo dailyBalance) {
        List<DailyBalance> itemList = new ArrayList<>();
        if (dailyBalance.getWarehouseId().equals("0")){
             itemList = service.list(new QueryWrapper<DailyBalance>()
                    .between("balance_date", DateUtils.format(dailyBalance.getBalanceDateStart(), "yyyy-MM-dd"),
                            DateUtils.format(dailyBalance.getBalanceDateEnd(), "yyyy-MM-dd")));
        }else {
            itemList = service.list(new QueryWrapper<DailyBalance>()
                    .between("balance_date", DateUtils.format(dailyBalance.getBalanceDateStart(), "yyyy-MM-dd"),
                            DateUtils.format(dailyBalance.getBalanceDateEnd(), "yyyy-MM-dd")));
        }



        DailyBalance balance = new DailyBalance();
        balance
                .setAmountBeginSum(itemList.stream().mapToDouble(o -> o.getAmountBeginSum() == null ? 0.0 : o.getAmountBeginSum()).sum())
                .setQtyBeginSum(itemList.stream().mapToDouble(o -> o.getQtyBeginSum() == null ? 0.0 : o.getQtyBeginSum()).sum())
                .setAmountEndSum(itemList.stream().mapToDouble(o -> o.getAmountEndSum() == null ? 0.0 : o.getAmountEndSum()).sum())
                .setQtyEndSum(itemList.stream().mapToDouble(o -> o.getQtyEndSum() == null ? 0.0 : o.getQtyEndSum()).sum())
                .setQtyConsumeOutSum(itemList.stream().mapToDouble(o -> o.getQtyConsumeOutSum() == null ? 0.0 : o.getQtyConsumeOutSum()).sum())
                .setAmountConsumeOutSum(itemList.stream().mapToDouble(o -> o.getAmountConsumeOutSum() == null ? 0.0 : o.getAmountConsumeOutSum()).sum())
                .setQtyOutSum(itemList.stream().mapToDouble(o -> o.getQtyOutSum() == null ? 0.0 : o.getQtyOutSum()).sum())
                .setAmountOutSum(itemList.stream().mapToDouble(o -> o.getAmountOutSum() == null ? 0.0 : o.getAmountOutSum()).sum())
                .setAmountInSum(itemList.stream().mapToDouble(o -> o.getAmountInSum() == null ? 0.0 : o.getAmountInSum()).sum())
                .setQtyInSum(itemList.stream().mapToDouble(o -> o.getQtyInSum() == null ? 0.0 : o.getQtyInSum()).sum())
                .setAmountChargingSum(itemList.stream().mapToDouble(o -> o.getAmountChargingSum() == null ? 0.0 : o.getAmountChargingSum()).sum())
                .setQtyChargingSum(itemList.stream().mapToDouble(o -> o.getQtyChargingSum() == null ? 0.0 : o.getQtyChargingSum()).sum());
        return RestResult.ok("数据查询成功",balance);
    }
}
